// import Dialog from '@vant/weapp/dialog/dialog';
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog'
Page({
  data: {
    // 房屋列表
    list: []
  },
  onLoad () {
    this.getHouseList()
    Dialog.confirm({
      title: '标题',
      message: '弹窗内容',
    })
      .then(() => {
        // on confirm
      })
      .catch(() => {
        // on cancel
      });
  },

  // 获取房屋列表数据
  async getHouseList () {
    const { data } = await wx.http.get('/room')
    console.log('房屋列表：', data)
    this.setData({
      list: data
    })
  },

  deleteHouse (ev) {
    const { position, instance } = ev.detail
    console.log(2, position)  
    if (position === 'right') {
      // 显示 Dialog 对话框
      // 执行房屋删除
      wx.showModal({
        title: '确认',
        content: '确认删除房屋吗？',
        success: (res) => {
          if (res.confirm) {
            console.log('用户点击确定')
            wx.http.delete(`/room/${ev.mark.id}`)
            // 删除之后刷新列表
            // this.getHouseList()
            this.setData({
              // 通过filter过滤删除的房子，重新setData刷新列表
              list: this.data.list.filter(item => item.id !== ev.mark.id)
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })

      // swiper-cell 滑块关闭
      instance.close()
    }
  },

  goDetail (ev) {
    wx.navigateTo({
      url: `/house_pkg/pages/detail/index?id=${ev.mark.id}`,
    })
  },

  addHouse () {
    wx.navigateTo({
      url: '/house_pkg/pages/locate/index',
    })
  },
})
